package network;

import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


public class Percent {
	public static final String FRIENDS = "select fid,fname,relation from  wb_twoway_relationship where uid=?";
//	public static final String FRIENDS_TAG = "select uid,tag,type from wb_celebrity where ";
	public static final String FRIENDS_TAG = "select uid,tag,type from wb_celebrity where type=\'1\' and (";
	
	public static final String FRIENDS_INCOMMON = "select oid,fid from wb_twoway_common where uid=?";
	public static String ALLTAG = "其他/";
	private String m_uid  = "";
	private double fsize = 0;
	private String m_screenName = "";
	public Percent(String uid,String name){
		m_uid = uid;
		m_screenName  = name;
	}
	
	public List<Map<String,Object>> getFriendById(){
		Database con = Database.getInstance();
		String[] objPm = {m_uid};
		try {
			return con.query(FRIENDS,objPm);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null ;
	
	}
	
	public List<Map<String, Object>> getFriendsTag(List<Map<String,Object>> friends){
		String tmpSqlWhere = "";
		for(Map<String,Object> friend : friends){
			tmpSqlWhere +="uid = \'"+friend.get("fid")+"\' or ";
		}
		if(tmpSqlWhere!=""){
			tmpSqlWhere = tmpSqlWhere.substring(0,tmpSqlWhere.length()-3);
			Database con = Database.getInstance();
			try {
				return con.query(FRIENDS_TAG + tmpSqlWhere+")");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null ;
	}
	
	public List<Map<String, Object>> getIncommonById(){
		Database con = Database.getInstance();
		String[] objPm = {m_uid};
		try {
			return con.query(FRIENDS_INCOMMON,objPm);
		} catch (SQLException e) {
			e.printStackTrace();
		}	
		return null ;
	}
	
	public List<Map<String,Object>> diffuseByTag() throws IOException{
		List<Map<String,Object>> friends =  getFriendById();		
		List<Map<String,Object>> tags = getFriendsTag(friends);
		List<Map<String,Object>> icms = getIncommonById();
		Map<String,List<Map<String,Object>>> icmfs =  new HashMap<String,List<Map<String,Object>>>();
		List<Map<String,Object>> friensTag = new ArrayList<Map<String,Object>>(1);
		fsize = friends.size();
		
		for(Map<String,Object> icm :icms){
			if(icmfs.get(icm.get("oid").toString())==null){
				List<Map<String,Object>>temp = new ArrayList<Map<String,Object>>(1);
				temp.add(icm);
				icmfs.put(icm.get("oid").toString(),temp);
			}else{
				icmfs.get(icm.get("oid").toString()).add(icm);
			}
		}
		FileWriter fw = new FileWriter("D:\\workspace\\score\\"+m_screenName+"_tags.txt",false);
		for(Map<String,Object> friend : friends){
			List<Map<String,Object>> friendIcms = icmfs.get(friend.get("fid").toString());
			String tempTag = "";
			if(friendIcms!=null){
				for(Map<String,Object> ficm : friendIcms){
					for(Map<String,Object> tag : tags){
						if(tag.get("uid").toString().equals(ficm.get("oid").toString())){
							tempTag =  tag.get("tag").toString().split("/")[0]+"/";
							break;
						}
						if(tag.get("uid").toString().equals(ficm.get("fid").toString())){
							String[] tmptags = tag.get("tag").toString().split("/");
							if(tempTag.indexOf(tmptags[0])==-1)
								tempTag +=tmptags[0]+"/";
							if(ALLTAG.indexOf(tmptags[0])==-1)
								ALLTAG+=tmptags[0]+"/";
						}
						
					}
				}
			}
			if(tempTag==""){
				tempTag="其他/";
				for(Map<String,Object> tag : tags){
					if(tag.get("uid").toString().equals(friend.get("fid").toString())){
						tempTag =  tag.get("tag").toString().split("/")[0]+"/";
						break;
					}
				}
			}
			tempTag = sort(tempTag);
			friend.put("tag",tempTag);
			
			friensTag.add(friend);
			fw.write(friend.get("fid").toString()+"  "+
					friend.get("fname").toString()+"  "+
					tempTag+"  "+
					friend.get("relation") + "\r\n");
			log(friend);
		}
		fw.close();
		return friensTag;
	}
	public void  getPercent() throws IOException{
		List<Map<String,Object>> pff = diffuseByTag();
		Map <String,Integer> tagPercent = new HashMap<String,Integer>(1);
		FileWriter fw = new FileWriter("D:\\workspace\\score\\"+m_screenName+"_diffTag.txt",false);
		for(String tag : ALLTAG.split("/")){
			int index = 0;
			for(Map<String,Object> ft : pff){
				for(String ftag :ft.get("tag").toString().split("/")){
					if(ftag.equals(tag))
						index++;
				}
			}
			fw.write(tag +" "+index+" "+index/fsize+"\r\n");
			System.out.print(index/fsize);
			System.out.println(" "+tag +" "+index+" ");
			tagPercent.put(tag, index);
		}
		fw.close();
	}
	public void log(Object str){
		System.out.println(str);
	}
	private String sort(String tempTag){
		String [] score = tempTag.split("/");
		List<String> lt = new ArrayList<String>();
		for(String str : score)
			lt.add(str);
		Collections.sort(lt);
		String sortTag = "";
		for(String str:lt)
			sortTag += str+"/";
		return sortTag;
	}
	public static void main (String[] argvs){
		//"1912958980","yuxing"
		//"1634074550","yuquan"
		//"1778830754","huangyu"
		//"1765769663","dongping"
		//"2008266387","qianming"
		//"1913694965", "anjiaxiaonuo"
		//"1877540477", "zico"
		//"1401659004", "zhanghao"		
		Percent pt = new Percent("2036517924","yulai");
		try {
			pt.getPercent();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
